using System;
using System.Collections.Generic;
using System.Text;
using Astro.Core;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using System.Web;
using System.Text.RegularExpressions;

namespace Astro.Plus.PlugIn
{
    public class GoDaddyLink : AstroEvent
    {
        HttpContext context = HttpContext.Current;

        #region EditableForm members

        public override string Name
        {
            get
            {
                return "GoDaddy Url";
            }
        }

        public override string Description
        {
            get
            {
                return "Solution for rewriter url when use multiple hosting on Godady";
            }
        }

        public override bool IsEditable
        {
            get
            {
                return true;
            }
        }
        #endregion

        #region Properties
        private string _godaddyFolder = "/sample";
        public string GoDaddyFolder
        {
            get { return _godaddyFolder; }
            set { _godaddyFolder = value; }
        }
        #endregion

        #region Configuration

        protected override FormElementCollection AddFormElements()
        {
            FormElementCollection fec = new FormElementCollection();
            fec.Add(new TextFormElement("godaddyFolder", "GoDaddy folder", "Folder of website on GoDaddy when use mutiple Windows Hosting"));

            return fec;
        }

        protected override NameValueCollection DataAsNameValueCollection()
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc["godaddyFolder"] = GoDaddyFolder;

            return nvc;
        }

        public override StatusType SetValues(HttpContext context, NameValueCollection nvc)
        {
            GoDaddyFolder = nvc["godaddyFolder"];

            return StatusType.Success;
        }
        #endregion

        #region AstroEvent members

        public override void Init(AstroApplication ga)
        {
            ga.AfterLoadContent += new DataObjectEventHandler(ga_AfterLoadContent);
        }

        void ga_AfterLoadContent(DataBuddyBase dataObject, EventArgs e)
        {
            string newUrl;
            Post post = dataObject as Post;
            HttpRequest request = HttpContext.Current.Request; 

            if (post != null)
            {
                newUrl = string.Format("{0}://{1}{2}{3}",
                            request.Url.Scheme, 
                            request.Url.Host, 
                            request.Url.Port == 80 ? string.Empty : ":" + request.Url.Port, 
                            post.Url);

                post.Url = newUrl.Replace(GoDaddyFolder, "");

                return;
            }

            Category category = dataObject as Category;
            if (category != null)
            {
                newUrl = string.Format("{0}://{1}{2}{3}",
                            request.Url.Scheme,
                            request.Url.Host,
                            request.Url.Port == 80 ? string.Empty : ":" + request.Url.Port,
                            category.Url);

                category.Url = newUrl.Replace(GoDaddyFolder, "");
            }
        }
        #endregion
    }
}
